package com.zhuozhou.jdyc.main.meinfo.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.main.goods.bean.TiHuoBase;
import com.zhuozhou.jdyc.main.main.bean.BasicsCitySearchBean;
import com.zhuozhou.jdyc.main.meinfo.bean.BangDingBean;
import com.zhuozhou.jdyc.main.meinfo.bean.BasicsMyPayBean;
import com.zhuozhou.jdyc.main.meinfo.bean.CheckBoxBean;
import com.zhuozhou.jdyc.main.meinfo.bean.CollectBean;
import com.zhuozhou.jdyc.main.meinfo.bean.ConcernMerchantBean;
import com.zhuozhou.jdyc.main.meinfo.bean.DaiLiShangBean;
import com.zhuozhou.jdyc.main.meinfo.bean.FXOrderBean;
import com.zhuozhou.jdyc.main.meinfo.bean.FenXaoBean;
import com.zhuozhou.jdyc.main.meinfo.bean.MsgBean;
import com.zhuozhou.jdyc.main.meinfo.bean.MyDiscountBean;
import com.zhuozhou.jdyc.main.meinfo.bean.QuYuBean;
import com.zhuozhou.jdyc.main.meinfo.bean.ShippingAddressBean;
import com.zhuozhou.jdyc.main.meinfo.bean.SpoorBean;
import com.zhuozhou.jdyc.main.meinfo.bean.WalletBean;
import com.zhuozhou.jdyc.main.user.bean.IndexMeInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Create by Once_ChuXy on 2021/9/18
 * Describe:我的信息
 */
public class MeInfoViewModel extends BaseViewModel {
    public MeInfoViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<BasicsMyPayBean> BasicsMyPayMutableLiveData = new MutableLiveData<>();

    public void getBasicsMyPay(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBasicsMyPay(map), true, BasicsMyPayMutableLiveData);
    }

    public MutableLiveData<BasicsCitySearchBean> BasicsPayRefundMutableLiveData = new MutableLiveData<>();

    public void postBasicsPayRefund(int admin_id, int commodity_id, String content, String imgid, String order_id, String out_trade_no,
                                    String price, String pricecount, int type, int user_id) {
        Map map = new HashMap();
        map.put("admin_id", admin_id);
        map.put("commodity_id", commodity_id);
        map.put("content", content);
        map.put("imgid", imgid);
        map.put("order_id", order_id);
        map.put("out_trade_no", out_trade_no);
        map.put("price", price);
        map.put("pricecount", pricecount);
        map.put("type", type);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postBasicsPayRefund(map), true, BasicsPayRefundMutableLiveData);
    }

    public MutableLiveData<BasicsCitySearchBean> BasicsConsigneeMutableLiveData = new MutableLiveData<>();

    public void postBasicsConsignee(int payid, int user_id) {
        Map map = new HashMap();
        map.put("pay_id", payid);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postBasicsConsignee(map), true, BasicsConsigneeMutableLiveData);
    }

    public MutableLiveData<BasicsCitySearchBean> BasicsDelPayMutableLiveData = new MutableLiveData<>();

    public void postBasicsDelPay(int payid, int user_id) {
        Map map = new HashMap();
        map.put("pay_id", payid);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postBasicsDelPay(map), true, BasicsDelPayMutableLiveData);
    }

    public MutableLiveData<IndexMeInfo> BasicsStatisticsMutableLiveData = new MutableLiveData<>();

    public void getBasicsStatistics(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBasicsStatistics(map), true, BasicsStatisticsMutableLiveData);
    }

    public MutableLiveData<WalletBean> BasicsWalletMutableLiveData = new MutableLiveData<>();

    public void getBasicsWallet(int type, String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("type", type);
        observe(apiServiceHelper.getBasicsWallet(map), true, BasicsWalletMutableLiveData);
    }
    public MutableLiveData<BasicsCitySearchBean> MoneyWalletMutableLiveData = new MutableLiveData<>();
    //timetable/basics/money
    public void postMoney(String price, int user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("price", price);
        observe(apiServiceHelper.postMoney(map), true, MoneyWalletMutableLiveData);
    }

    //timetable/Basics/footprint
    public MutableLiveData<SpoorBean> BasicsFootprintMutableLiveData = new MutableLiveData<>();

    public void getBasicsFootprint(int type, String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("type", type);
        observe(apiServiceHelper.getBasicsFootprint(map), true, BasicsFootprintMutableLiveData);
    }
    //timetable/Basics/myCollection

    public MutableLiveData<List<CollectBean>> BasicsMyCollectionMutableLiveData = new MutableLiveData<>();

    public void getBasicsMyCollection(int type, String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("type", type);
        observe(apiServiceHelper.getBasicsMyCollection(map), true, BasicsMyCollectionMutableLiveData);
    }

    //timetable/Basics/mymyfollow
    public MutableLiveData<ConcernMerchantBean> BasicsMyFollowMutableLiveData = new MutableLiveData<>();

    public void getBasicsMyFollow(Double y, Double x, String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("y", y);
        map.put("x", x);
        observe(apiServiceHelper.getBasicsMyFollow(map), true, BasicsMyFollowMutableLiveData);
    }

    //timetable/shop/mydiscount
    public MutableLiveData<List<MyDiscountBean>> BasicsMyDiscountMutableLiveData = new MutableLiveData<>();

    public void getMyDiscount(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getMyDiscount(map), true, BasicsMyDiscountMutableLiveData);
    }

    //timetable/basics/fenxiaomoney
    public MutableLiveData<FenXaoBean> BasicsFenXaoMoneyMutableLiveData = new MutableLiveData<>();

    public void getFenXaoMoney(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getFenXaoMoney(map), true, BasicsFenXaoMoneyMutableLiveData);
    }

    //timetable/Basics/fxorder
    public MutableLiveData<FXOrderBean> BasicsFXOrderMutableLiveData = new MutableLiveData<>();

    public void getFXOrder(String user_id, String status) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("status", status);
        observe(apiServiceHelper.getFXOrder(map), true, BasicsFXOrderMutableLiveData);
    }

    //timetable/Basics/txmoney
    public MutableLiveData<FXOrderBean> BasicsTXMoneyMutableLiveData = new MutableLiveData<>();

    public void getTXMoney(String user_id, String status) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("status", status);
        observe(apiServiceHelper.getTXMoney(map), true, BasicsTXMoneyMutableLiveData);
    }

    //timetable/Basics/fxuser2
    public MutableLiveData<FXOrderBean> BasicsFXUser2MutableLiveData = new MutableLiveData<>();

    public void getFXUser2(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getFXUser2(map), true, BasicsFXUser2MutableLiveData);
    }

    //timetable/Basics/txmoney
    public MutableLiveData<FXOrderBean> BasicsFXUser1MutableLiveData = new MutableLiveData<>();

    public void getFXUser1(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getFXUser1(map), true, BasicsFXUser1MutableLiveData);
    }

    //login/duanxin
    public MutableLiveData<MsgBean> DanXinMutableLiveData = new MutableLiveData<>();

    public void getDanXin(String phone, String user_id) {
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getDanXin(map), true, DanXinMutableLiveData);
    }

    //login/verify
    public MutableLiveData<Integer> VerifyMutableLiveData = new MutableLiveData<>();

    public void getVerify(String code, String phone, String user_id) {
        Map map = new HashMap();
        map.put("code", code);
        map.put("phone", phone);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getVerify(map), true, VerifyMutableLiveData);
    }

    //login/bangding
    public MutableLiveData<BangDingBean> BangDingMutableLiveData = new MutableLiveData<>();

    public void getBangDing(String pass, String phone, String user_id) {
        Map map = new HashMap();
        map.put("pass", pass);
        map.put("phone", phone);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBangDing(map), true, BangDingMutableLiveData);
    }

    //Basics/peisong
    public MutableLiveData<CheckBoxBean> PeiSongMutableLiveData = new MutableLiveData<>();

    public void getPeiSong() {
        Map map = new HashMap();
        observe(apiServiceHelper.getPeiSong(map), true, PeiSongMutableLiveData);
    }

    //Basics/peisong
    public MutableLiveData<QuYuBean> QuYuMutableLiveData = new MutableLiveData<>();

    public void getQuYu() {
        Map map = new HashMap();
        observe(apiServiceHelper.getQuYu(map), true, QuYuMutableLiveData);
    }

    //timetable/Basics/dianpuleixing
    public MutableLiveData<CheckBoxBean> DianPuLeiXingMutableLiveData = new MutableLiveData<>();

    public void getDianPuLeiXing() {
        Map map = new HashMap();
        observe(apiServiceHelper.getDianPuLeiXing(map), true, DianPuLeiXingMutableLiveData);
    }

    //timetable/Basics/weixin
    public MutableLiveData<CheckBoxBean> WeiXinMutableLiveData = new MutableLiveData<>();

    public void getWeiXin(String value, Boolean isShowDialog) {
        Map map = new HashMap();
        map.put("value", value);
        observe(apiServiceHelper.getWeiXin(map), isShowDialog, WeiXinMutableLiveData);
    }

    //timetable/Basics/dailishang
    public MutableLiveData<BasicsCitySearchBean> DaiLiShangMutableLiveData = new MutableLiveData<>();

    public void postDaiLiShang(DaiLiShangBean bean) {
        observe(apiServiceHelper.postDaiLiShang(bean), true, DaiLiShangMutableLiveData);
    }

    //timetable/Basics/updateimg
    public MutableLiveData<BasicsCitySearchBean> updateImgMutableLiveData = new MutableLiveData<>();

    public void postFile(String url, File file) {
        Map<String, RequestBody> paramMap = new HashMap<>();
        addMultiPart(paramMap, "file", file);
        observe(apiServiceHelper.postFile(url, paramMap), true, updateImgMutableLiveData);
    }

    //timetable/Basics/updateimg
    public MutableLiveData<BasicsCitySearchBean> DentityMutableLiveData = new MutableLiveData<>();

    public void postDentity(String url, File file) {
        Map<String, RequestBody> paramMap = new HashMap<>();
        addMultiPart(paramMap, "file", file);
        observe(apiServiceHelper.postFile(url, paramMap), true, DentityMutableLiveData);
    }

    //timetable/Basics/updateimg
    public MutableLiveData<BasicsCitySearchBean> DianpuimgMutableLiveData = new MutableLiveData<>();

    public void postDianpuimg(String url, File file) {
        Map<String, RequestBody> paramMap = new HashMap<>();
        addMultiPart(paramMap, "file", file);
        observe(apiServiceHelper.postFile(url, paramMap), true, DianpuimgMutableLiveData);
    }

    //timetable/Basics/updateimg
    public MutableLiveData<BasicsCitySearchBean> ImagesMutableLiveData = new MutableLiveData<>();

    public void postImages(String url, File file) {
        Map<String, RequestBody> paramMap = new HashMap<>();
        addMultiPart(paramMap, "file", file);
        observe(apiServiceHelper.postFile(url, paramMap), true, ImagesMutableLiveData);
    }

    //timetable/Basics/updateimg
    public MutableLiveData<BasicsCitySearchBean> updateImgsMutableLiveData = new MutableLiveData<>();

    public void postFiless(List<File> list) {
        observe(apiServiceHelper.uploadFilesWithParts(filesToMultipartBodyParts(list)), true, updateImgsMutableLiveData);
    }


    //timetable/Basics/mySite
    public MutableLiveData<ShippingAddressBean> MySiteMutableLiveData = new MutableLiveData<>();

    public void getMySite(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getMySite(map), true, MySiteMutableLiveData);
    }
    //timetable/Basics/siteDel
    public MutableLiveData<BasicsCitySearchBean> SiteDelMutableLiveData = new MutableLiveData<>();

    public void getSiteDel(int id, String user_id) {
        Map map = new HashMap();
        map.put("id", id);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getSiteDel(map), true, SiteDelMutableLiveData);
    }

    //timetable/Basics/site
    public MutableLiveData<BasicsCitySearchBean> SiteMutableLiveData = new MutableLiveData<>();

    public void postSite(String user_id, String name, String phone, String postcode, String city, String city_details, int status) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        map.put("name", name);
        map.put("phone", phone);
        map.put("postcode", postcode);
        map.put("city", city);
        map.put("city_details", city_details);
        map.put("status", status);
        observe(apiServiceHelper.postSite(map), true, SiteMutableLiveData);
    }

    //timetable/Basics/siteUpdate
    public MutableLiveData<BasicsCitySearchBean> SiteUpdateMutableLiveData = new MutableLiveData<>();

    public void postSiteUpdate(int id, String user_id, String name, String phone, String postcode, String city, String city_details, int status) {
        Map map = new HashMap();
        map.put("id", id);
        map.put("user_id", user_id);
        map.put("name", name);
        map.put("phone", phone);
        map.put("postcode", postcode);
        map.put("city", city);
        map.put("city_details", city_details);
        map.put("status", status);
        observe(apiServiceHelper.postSiteUpdate(map), true, SiteUpdateMutableLiveData);
    }


    /**
     * 添加多媒体类型
     *
     * @param paramMap 参数对
     * @param key      键
     * @param obj      值
     */
    private static void addMultiPart(Map<String, RequestBody> paramMap, String key, Object obj) {
        if (obj instanceof String) {
            RequestBody body = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), (String) obj);
            paramMap.put(key, body);
        } else if (obj instanceof File) {
            RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), (File) obj);
            paramMap.put(key + "\"; filename=\"" + ((File) obj).getName() + "", body);
        }
    }

    /**
     * 添加多媒体类型
     *
     * @param paramMap 参数对
     * @param key      键
     * @param list     值
     */
    private static void addMultiParts(Map<String, RequestBody> paramMap, String key, List<File> list) {
        for (File file : list) {
            RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), (File) list);
            paramMap.put(key + "\"; filename=\"" + ((File) file).getName() + "", body);
        }
    }

    public static MultipartBody filesToMultipartBody(List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            builder.addFormDataPart("file", file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    public static List<MultipartBody.Part> filesToMultipartBodyParts(List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }
}
